package camid.util.dataanalysis;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.SortedMap;
import java.util.TreeMap;

public class JMeterAnalyser {
	
	public static final String sample = "1363155604825,3958,Java Request,,,TG-20-500 1-8,,true,0,0";
	
	private SortedMap<JMeterTG, JMeterMetricSet> metricSets;
	
	public JMeterAnalyser()
	{
		metricSets = new TreeMap<JMeterTG, JMeterMetricSet> ();
	}
	
	public void analyseStream( InputStream is )
	{
		Scanner scanner = new Scanner(is);
		while(scanner.hasNext())
		{
			String line = scanner.nextLine();
			this.analyseLine(line);
		}
	}
	
	public void analyseLine( String line )
	{
		String[] parts = line.split(",");
		Long timestamp = 0L;
		Long responseTime = 0L;
		boolean success = false;
		if(parts[0] != null && !parts[0].equals(""))
		{
			timestamp = Long.parseLong(parts[0]);
		}
		if(parts[1] != null && !parts[1].equals(""))
		{
			responseTime = Long.parseLong(parts[1]);
		}
		if(parts[7] != null && !parts[7].equals(""))
		{
			success = Boolean.parseBoolean(parts[7]);
		}
//		System.out.println("tstamp " + timestamp);
//		System.out.println("rtime " + responseTime);
		JMeterTG tg = analyseTGString(parts[5]);
		JMeterMetric metric = new JMeterMetric();
		metric.setTg(tg);
		metric.setTimestamp(timestamp);
		metric.setResponseTime(responseTime);
		metric.setSuccess(success);
		addJMeterMetric(metric);
	}
	
	public JMeterTG analyseTGString( String s )
	{
		String[] parts = s.split(" ");
		parts = parts[0].split("-");
		Integer noObj = Integer.parseInt(parts[1]);
		Integer delay = Integer.parseInt(parts[2]);
//		System.out.println("noobj " + noObj);
//		System.out.println("delay " + delay);
		JMeterTG tg = new JMeterTG(noObj, delay);
		return tg;
	}
	
	public void addJMeterMetric(JMeterMetric metric)
	{
		JMeterMetricSet set = null;
		if(this.metricSets.containsKey(metric.getTg()))
		{
			set = this.metricSets.get(metric.getTg());
		}
		else
		{
			set = new JMeterMetricSet(metric.getTg());
			this.metricSets.put(metric.getTg(), set);
		}
		
		set.getSet().add(metric);
	}
	
	
	
	public void toStream(OutputStream os )
	{
		PrintWriter writer = new PrintWriter(os, true);
		for(Entry<JMeterTG, JMeterMetricSet> entry : this.metricSets.entrySet() )
		{
			JMeterMetricSet metricSet = entry.getValue();
			for(JMeterMetric metric : metricSet.getSet())
			{
				writer.println(metric.toCsvString());
			}
		}
	}
	
	public SortedMap<JMeterTG, JMeterMetricSet> getMetricSets() 
	{
		return metricSets;
	}

	public static void main(String[] args) throws FileNotFoundException 
	{
		JMeterAnalyser analyser = new JMeterAnalyser();
		
		FileInputStream fis = new FileInputStream("results/31-03-2013_19-35_local_migration/expMiddleware.log");
		analyser.analyseStream(fis);
		
		for(Entry<JMeterTG, JMeterMetricSet> entry : analyser.getMetricSets().entrySet())
		{
			JMeterMetricSet metricSet = entry.getValue();
			JMeterTimestampAnalyser tstampAnalyser = new JMeterTimestampAnalyser(metricSet);
			tstampAnalyser.analyse();
			tstampAnalyser.toStream(System.out);
		}
		
		System.out.println("#################");
		
		analyser.toStream(System.out);
		
	}

}
